package com.educate.ucenter.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.educate.common.web.R;
import com.educate.ucenter.model.bo.AccountBO;
import com.educate.ucenter.model.bo.UserBO;
import com.educate.ucenter.rpc.api.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}")
    private UserService userService;

    @PostMapping(value = "/create")
    public R create(@RequestBody UserBO bo) {
        userService.create(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    public R delete(@RequestBody UserBO bo) {
        userService.delete(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete/{userId}")
    public R delete(@PathVariable String userId) {
        userService.delete(userId);
        return R.ok();
    }

    @PostMapping(value = "/detail/{userId}")
    public R<UserBO> detail(@PathVariable String userId) {
        UserBO bo = userService.detail(userId);
        return R.ok(bo);
    }

    @PostMapping(value = "/detail")
    public R<UserBO> detail(@RequestBody UserBO bo) {
        UserBO detail = userService.detail(bo);
        return R.ok(detail);
    }

    @PostMapping(value = "/update")
    public R<UserBO> update(@RequestBody UserBO bo) {
        userService.update(bo);
        return R.ok();
    }

    @PostMapping(value = "/query")
    public R<Page<UserBO>> query(@RequestBody UserBO bo) {
        Page<UserBO> result = userService.query(bo);
        return R.ok(result);
    }

    @PostMapping(value = "/list")
    public R<List<UserBO>> list(@RequestBody UserBO bo) {
        List<UserBO> result = userService.list(bo);
        return R.ok(result);
    }

    /**
     * 获取微信小程序的登录session
     */
    @PostMapping(value = "/wxAppletSession")
    public R<String> wxAppletSession(@RequestBody UserBO bo) {
        String sessionId = userService.wxAppletSession(bo);
        return R.ok(sessionId);
    }

    /**
     * 微信小程序登录
     */
    @RequestMapping(value = "/wxAppletLogin")
    public R<UserBO> wxAppletLogin(@RequestBody AccountBO bo) {
        UserBO result = userService.wxAppletLogin(bo);
        return R.ok(result);
    }
}
